package _04高效流;

import java.io.*;
import java.util.Calendar;

public class BufferedStreamDemo {

    public static void main(String[] args) throws IOException {

        copyFile("./materials/1.txt", "./materials/2.txt");

    }

    private static void copyFile(String s1, String s2) throws IOException {

        // 1. 打开流
        InputStream is = new BufferedInputStream(new FileInputStream(s1));
        OutputStream os = new BufferedOutputStream(new FileOutputStream(s2));

        // 2. 边写边读

        Calendar c1 = Calendar.getInstance();

//		// 1) 读一个字节，写一个字节
//		int data;
//		do {
//			// 从输入流读一个字节
//			data = is.read();
//			// 写入到输出流中
//			os.write(data);
//		} while (data != -1);
//

        // 2) 读1K，写1K
        byte[] data2 = new byte[1024];
        int len = 0;
        do {

            // 从输入流中读取
            len = is.read(data2);
            if (len > 0) {
                os.write(data2, 0, len);
            }
        } while (len != -1);

        Calendar c2 = Calendar.getInstance();
        System.out.println(c2.getTimeInMillis() - c1.getTimeInMillis() + "ms");

        // 3. 关闭流
        is.close();
        os.close();

    }

}